package com.chebaohui.iqs.service.impl.deal.libao.profess;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chebaohui.iqs.config.CompanyCode;
import com.chebaohui.iqs.config.CompanyValue;
import com.chebaohui.iqs.config.ResultCode;
import com.chebaohui.iqs.entity.base.Cars;
import com.chebaohui.iqs.entity.req.CheckCar;
import com.chebaohui.iqs.entity.res.CompanyCars;
import com.chebaohui.iqs.service.impl.deal.libao.sdk.LibaoRequest;
import com.chebaohui.iqs.service.impl.deal.libao.util.LibaoConfig;
import com.chebaohui.iqs.service.impl.deal.libao.util.PublicParam;
import com.chebaohui.iqs.service.impl.deal.util.StringUtil;

/**
 * 利宝车辆查询类
 * 
 * @author regas_xie
 *
 */
public class LbCheckCar {
	/**
	 * log:日志输出
	 */
	private static Log log = LogFactory.getLog(LbCheckCar.class);

	/**
	 * 利宝查车
	 * 
	 * @param carParam
	 *            参数
	 * @return
	 */
	public static CompanyCars<List<Cars>> tpCheckCar(CheckCar carParam) {
		log.info("进入" + CompanyValue.LIBAO + "查询车型" + carParam.getLicense().getLableNo() + "....");
		try {
			// Map<String, String> map = PublicParam.getParam();
			JSONArray motorList = checkCar(carParam.getLicense().getLableNo(), PublicParam.getParam());
			if (null == motorList || motorList.isEmpty() || motorList.size() < 1) {
				return new CompanyCars<List<Cars>>(CompanyCode.LIBAO.getCode(), ResultCode.N04.getCode(),
						ResultCode.N04.getMsg() + "无此厂牌车辆信息");
			}

			CompanyCars<List<Cars>> result = new CompanyCars<List<Cars>>(CompanyCode.LIBAO.getCode(),
					ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
			result.setCars(dealCars(motorList));
			return result;
		} catch (Exception e) {
			log.error(CompanyValue.LIBAO + "查询车型" + carParam.getLicense().getLableNo() + "过程过程异常:" + e.getMessage());
			return new CompanyCars<List<Cars>>(CompanyCode.LIBAO.getCode(), ResultCode.N02.getCode(),
					ResultCode.N02.getMsg() + "查询车型过程异常:" + e.getMessage());
		} finally {
			log.info(CompanyValue.LIBAO + "查询车型" + carParam.getLicense().getLableNo() + "结束。");
		}
	}

	/**
	 * 查车
	 * 
	 * @param lableNo
	 * @return
	 * @throws Exception
	 */
	public static JSONArray checkCar(String lableNo, Map<String, String> map) throws Exception {
		log.info("进入" + CompanyValue.LIBAO + "查询车型" + lableNo + "....");

		if (StringUtil.isNulls(map.get("access_token"))) {
			throw new Exception("利宝token认证获取失败");
		}
		map.put("modelName", lableNo);
		JSONObject json = PublicParam.getParam(map);
		json.put("pdk", PublicParam.getPdk(md5Map(map)));
		json.put("pageNum", 1);
		json.put("intPageCount", 100);
		String res = LibaoRequest.post(
				LibaoConfig.libao_url + "/query/queryVehicle?access_token=" + map.get("access_token"),
				json.toJSONString());

		log.info(CompanyValue.LIBAO + "查车:" + "\n" + "请求路径:" + LibaoConfig.libao_url
				+ "/query/queryVehicle?access_token=" + map.get("access_token") + "\n" + "请求参数:" + json.toJSONString()
				+ "\n" + "请求返回:" + res);

		if (StringUtil.isNulls(res))
			throw new Exception("请求返回空");
		JSONObject cars = JSONObject.parseObject(res);

		if (!cars.getBoolean("success")) {
			throw new Exception("车辆查询失败，利宝返回错误");
		}

		return cars.getJSONObject("result").getJSONArray("motorList");
	}

	/**
	 * 处理接口返回结果
	 * 
	 * @param list
	 *            车型列表
	 * @return
	 */
	private static List<Cars> dealCars(JSONArray list) {
		List<Cars> res = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			Cars c = new Cars();
			c.setModelCode(list.getJSONObject(i).getString("modeCode"));
			c.setModelName(list.getJSONObject(i).getString("modelName"));
			c.setExhausMeasur(list.getJSONObject(i).getString("exHaustScale"));
			c.setSaleDate(list.getJSONObject(i).getString("carYear"));
			c.setPurchase(list.getJSONObject(i).getString("purchasePrice"));
			c.setSeats(list.getJSONObject(i).getIntValue("seatCount"));
			c.setRemark(list.getJSONObject(i).getString("remark"));

			c.setOption(c.getRemark() + CompanyValue.SEG + c.getModelCode() + CompanyValue.SEG + c.getModelName()
					+ CompanyValue.SEG + c.getExhausMeasur() + CompanyValue.SEG + c.getSaleDate() + CompanyValue.SEG
					+ c.getPurchase() + CompanyValue.SEG + c.getSeats());
			c.setRenewal("false");
			res.add(c);
		}

		return res;
	}
	
	private static Map<String, String> md5Map(Map<String, String> map) {
		Map<String, String> md5 = new HashMap<String, String>();
		md5.put("partnerAccountCode", map.get("partnerAccountCode"));
		md5.put("agreementNo", map.get("agreementNo"));
		md5.put("recordCode", map.get("recordCode"));
		md5.put("flowId", map.get("flowId"));
		md5.put("modelName", map.get("modelName"));
		return md5;
	}

}
